﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using efuture.utils;
using efuture.http;
using System.IO;

namespace efuture.vdrp
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm, IPlugin
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            treeList1.DataSource = null;
            treeList1.Nodes.Clear();

            String xmlData = HttpHelper.DoGet("/sys/role/find", null);
            DataSet ds = new DataSet();
            ds.ReadXmlSchema(Application.StartupPath + "\\Role.xsd");
            ds.ReadXml(new StringReader(xmlData));
            treeList1.DataSource = ds.Tables[0];
            ((DataTable)treeList1.DataSource).DataSet.AcceptChanges();
        }

        #region IPlugin 成员

        public void afterPropertiesSet(Dictionary<string, string> properties)
        {
            HttpHelper.serverURL = properties["serverURL"];
        }

        public void shutdown()
        {
            _ctx.OnEvent(EventType.OnClosed);
        }

        private IPluginContext _ctx;
        public StartupResult startup(IPluginContext ctx)
        {
            _ctx = ctx;
            return StartupResult.Success;
        }

        #endregion

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            
            treeList1.FocusedNode = treeList1.AppendNode(new Object[]{ }, null);
            treeList1.FocusedNode["RoleId"] = VdrpUtils.getIdentity();
            treeList1.FocusedNode["RoleEnable"] = "T";
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (treeList1.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)treeList1.DataSource;

            StringWriter sw = null;
            if (dt.DataSet.HasChanges(DataRowState.Added))
            {
                DataSet ds2 = dt.DataSet.GetChanges(DataRowState.Added);
                sw = new StringWriter();
                ds2.WriteXml(sw);
                HttpHelper.DoPost("/sys/role/add", sw.ToString(), null);
            }
            if (dt.DataSet.HasChanges(DataRowState.Modified))
            {
                DataSet ds2 = dt.DataSet.GetChanges(DataRowState.Modified);
                sw = new StringWriter();
                ds2.WriteXml(sw);
                HttpHelper.DoPost("/sys/role/update", sw.ToString(), null);
            }

            this.simpleButton1_Click(simpleButton1, EventArgs.Empty);
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (treeList1.FocusedNode == null)
            {
                return;
            }

            String RoleId = treeList1.FocusedNode["RoleId"].ToString();
            HttpHelper.DoGet(String.Format("/sys/role/delete?RoleId={0}", RoleId), null);

            this.simpleButton1_Click(simpleButton1, EventArgs.Empty);
        }
    }
}